﻿@using VideoSeller.Business.Data
@using VideoSeller.Business.Data.Enums
@model ClientsViewModel

@{
	ViewBag.Title = @Html.GetGlobalResource("PagesTitles", "AdminFinance");
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}

@functions {

	string LastRefill(Client client) {
		if (client.Finance != null && client.Finance.Postings.Count > 0) {
			var items = client.Finance.Postings.Where(x => x.Operation == (short)FinanceOperation.Refill).OrderByDescending(x => x.Date);

			if (items.Any()) {
                return @Html.GetCurrencyString(items.First().Value);//vladimir
			//	return string.Format("{0:0}", items.First().Value); 
			}
		}

		return "0";
	}

	string LastOperationDate(Client client)
	{
		if (client.Finance != null && client.Finance.Postings.Count > 0)
		{
			var items = client.Finance.Postings.OrderByDescending(x => x.Date);

			if (items.Any())
			{
				return string.Format("{0:0}", items.First().Date.ToString("dd MMMM yyyy HH:mm"));
			}
		}

		return string.Empty;
	}
}

<h2>@Html.GetGlobalResource("PagesTitles", "AdminFinance")</h2>
<table>
	<tr>
		<th>@Html.GetLocalResource("Login")</th>
		<th>@Html.GetLocalResource("Balance")</th>
		<th>@Html.GetLocalResource("LastRefill")</th>
		<th>@Html.GetLocalResource("LastOperationDate")</th>
		<th>@Html.GetLocalResource("Actions")</th>
	</tr>
	@foreach (var item in Model.Clients) {
		if (item.Finance != null) {
			<tr>
				<td>@item.FullName</td>
				<td>@Html.GetCurrencyString(item.Finance != null ? item.Finance.Total : 0)</td>
				<td>@LastRefill(item)</td>
				<td>@LastOperationDate(item)</td>
				<td>
					<input type="button" value="@Html.GetLocalResource("Add")" onclick="@string.Format("openAddFinanceWindow('{0}', '{1}');", item.UserId, Model.LoginFilter)" style="float: left;"/>
					<input type="button" value="@Html.GetLocalResource("Substract")" onclick="@string.Format("openSubstractFinanceWindow('{0}', '{1}');", item.UserId, Model.LoginFilter)" style="float: left;"/>
					@if (item.Finance.Active) {
						using (Html.BeginForm("BlockFinance", "Admin", new {userId = item.UserId, loginFilter = Model.LoginFilter}, FormMethod.Post, new {style = "float: left;"})) {
							<input type="submit" value="@Html.GetLocalResource("Block")"/>
						}
					}
					else {
						using (Html.BeginForm("UnblockFinance", "Admin", new {userId = item.UserId, loginFilter = Model.LoginFilter}, FormMethod.Post, new {style = "float: left;"})) {
							<input type="submit" value="@Html.GetLocalResource("Unblock")"/>
						}
					}
				</td>
			</tr>
		}
 }
</table>
<br/><br/>
@if (Model.FilterViewModel.Total > 50) {
	<ul class="paging">
		@Html.Paging(Model.FilterViewModel.PageNumber, Model.FilterViewModel.PageCount, x => Url.Action(Model.FilterViewModel.Action, Model.FilterViewModel.Controller, new
		{
			page = x,
			loginFilter = Model.LoginFilter
		}))
	</ul>
	<div class="ui-helper-clearfix"></div><br/>
}
@Html.Partial("_LoginFilter", new LoginFilterViewModel { Action = "Finance", Controller = "Admin" })

<div id="add-finance" title="@Html.GetLocalResource("AddFinancePopupTitle")">
	<form method="post" action="@Url.Action("AddFinance", "Admin")">
		<input id="userIdAdd" name="userIdAdd" type="hidden"/>
		<input id="loginFilterAdd" name="loginFilterAdd" type="hidden"/>
		<h3>@Html.GetLocalResource("EnterAmount")</h3>
		<input id="financeAdd" name="financeAdd" type="text"/>
		<input type="submit" value="@Html.GetLocalResource("Add")">
	</form>
</div>

<div id="substract-finance" title="@Html.GetLocalResource("SubstractFinancePopupTitle")">
	<form method="post" action="@Url.Action("SubstractFinance", "Admin")">
		<input id="userIdSubstract" name="userIdSubstract" type="hidden"/>
		<input id="loginFilterSubstract" name="loginFilterSubstract" type="hidden"/>
		<h3>@Html.GetLocalResource("EnterAmount")</h3>
		<input id="financeSubstract" name="financeSubstract" type="text"/>
		<input type="submit" value="@Html.GetLocalResource("Substract")">
	</form>
</div>